﻿Public Class FrmKhoHang
    Private flag As Boolean

    Private Sub FrmKhoHang_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadDSKH()
        LockButton(True)
        LockTextbox(False)
    End Sub

    'load danh sach len data table
    Sub LoadDSKH()
        Dim ds As DataTable
        ds = DanhSachKhoHang_DAO.SelectAll()
        dgvDSKhoHang.DataSource = ds
    End Sub

    'xóa trắng các textbox
    Sub ClearTextbox()
        txtMaKho.Text = ""
        txtTenKho.Text = ""
        txtDiaChiKho.Text = ""
    End Sub

    'đồng bộ các nút chức năng giống nhau
    Sub LockButton(ByVal bt As Boolean)
        btnThemKho.Enabled = bt
        btnSuaKho.Enabled = bt
        btnXoaKho.Enabled = bt
        btnLuuKho.Enabled = Not bt
        btnKhongLuuKho.Enabled = Not bt
        btnThoat.Enabled = bt
    End Sub

    'lock các textbox 
    Sub LockTextbox(ByVal tb As Boolean)
        txtMaKho.Enabled = tb
        txtTenKho.Enabled = tb
        txtDiaChiKho.Enabled = tb
        If tb = False Then
            txtMaKho.BackColor = Color.GreenYellow
            txtTenKho.BackColor = Color.GreenYellow
            txtDiaChiKho.BackColor = Color.GreenYellow
        Else
            txtMaKho.BackColor = Color.White
            txtTenKho.BackColor = Color.White
            txtDiaChiKho.BackColor = Color.White
        End If
    End Sub

    Sub Them()
        Dim ds As DanhSachKhoHang_DTO = New DanhSachKhoHang_DTO()
        ds.MaKho = txtMaKho.Text
        ds.TenKho = txtTenKho.Text
        ds.DiaChi = txtDiaChiKho.Text

        DanhSachKhoHang_DAO.Insert(ds)
        LoadDSKH()
    End Sub

    Sub Xoa()
        Dim ds As DanhSachKhoHang_DTO = New DanhSachKhoHang_DTO()
        ds.MaKho = txtMaKho.Text

        DanhSachKhoHang_DAO.Delete(ds)
        LoadDSKH()
    End Sub

    Sub Sua()
        Dim ds As DanhSachKhoHang_DTO = New DanhSachKhoHang_DTO()
        ds.MaKho = txtMaKho.Text
        ds.TenKho = txtTenKho.Text
        ds.DiaChi = txtDiaChiKho.Text

        DanhSachKhoHang_DAO.Update(ds)
        LoadDSKH()
    End Sub

    Private Function TestData() As Boolean
        TestData = True
        If Trim(txtMaKho.Text) = "" Then
            TestData = False
            MsgBox("Nhập mã kho !", MsgBoxStyle.Exclamation, "Thông Báo Lỗi")
            txtMaKho.Focus()
            Exit Function
        ElseIf Trim(txtTenKho.Text) = "" Then
            TestData = False
            MsgBox("Nhập tên kho !", MsgBoxStyle.Exclamation, "Thông Báo Lỗi")
            txtTenKho.Focus()
            Exit Function
        ElseIf Trim(txtDiaChiKho.Text) = "" Then
            TestData = False
            MsgBox("Nhập địa chỉ kho !", MsgBoxStyle.Exclamation, "Thông Báo Lỗi")
            txtDiaChiKho.Focus()
            Exit Function
        End If
    End Function

    'load các thuộc tính từ lưới lên textbox
    Sub HienThi()
        If (dgvDSKhoHang.SelectedRows.Count > 0) Then
            txtMaKho.Text = dgvDSKhoHang.SelectedRows(0).Cells(0).Value.ToString()
            txtTenKho.Text = dgvDSKhoHang.SelectedRows(0).Cells(1).Value.ToString()
            txtDiaChiKho.Text = dgvDSKhoHang.SelectedRows(0).Cells(2).Value.ToString()
        End If
    End Sub

    'xử lý sự kiện các nút
    Private Sub btnThemKho_Click(sender As Object, e As EventArgs) Handles btnThemKho.Click
        flag = True
        LockTextbox(True)
        LockButton(False)
        ClearTextbox()
        dgvDSKhoHang.Enabled = False
        txtMaKho.Focus()
    End Sub

    Private Sub btnSuaKho_Click(sender As Object, e As EventArgs) Handles btnSuaKho.Click
        flag = False
        LockTextbox(True)
        LockButton(False)
        dgvDSKhoHang.Enabled = False
        txtMaKho.Focus()
    End Sub

    Private Sub btnXoaKho_Click(sender As Object, e As EventArgs) Handles btnXoaKho.Click
        Xoa()
    End Sub

    Private Sub btnLuuKho_Click(sender As Object, e As EventArgs) Handles btnLuuKho.Click
        If TestData() Then
            If flag = True Then
                Them()
            Else
                Sua()
            End If
            LockTextbox(False)
            LockButton(True)
            dgvDSKhoHang.Enabled = True
            MsgBox("cập nhật dữ liệu thành công", MsgBoxStyle.Information, "Thông Báo !")
        End If
    End Sub

    Private Sub btnKhongLuuKho_Click(sender As Object, e As EventArgs) Handles btnKhongLuuKho.Click
        LoadDSKH()
        LockTextbox(False)
        LockButton(True)
        dgvDSKhoHang.Enabled = True
    End Sub

    Private Sub btnThoat_Click(sender As Object, e As EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub dgvDSKhoHang_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDSKhoHang.CellContentClick
        HienThi()
    End Sub

    Private Sub dgvDSKhoHang_SelectionChanged(sender As Object, e As EventArgs) Handles dgvDSKhoHang.SelectionChanged
        HienThi()
    End Sub
End Class